package com.digitalchina.lxnsy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.CartVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：购物车Adapter
 * 包名：com.digitalchina.bigdata.adapter
 * 创建人：白彦东
 * 创建时间：2016/12/30 15:16
 * 修改备注：
 * ================================================
 */

public class ConfirmOrdersAdapter extends BaseExpandableListAdapter {

    protected Context mContext;
    protected LayoutInflater layoutInflater;
    private List<CartVO> groupList;
    private List<List<CartVO.ShoppingCartItemVOListBean>> childList;
    private int mGroupPosition, mChildPosition;

    public ConfirmOrdersAdapter(Context mContext, List<CartVO> groupList, List<List<CartVO.ShoppingCartItemVOListBean>> childList) {
        this.mContext = mContext;
        this.groupList = groupList;
        this.childList = childList;
        layoutInflater = LayoutInflater.from(mContext);
    }


    public void setListData(List<CartVO> groupList, List<List<CartVO.ShoppingCartItemVOListBean>> childList) {
        this.groupList = groupList;
        this.childList = childList;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final ViewHolderGroup viewHolderGroup;
        if (convertView == null) {
            convertView = inflate(R.layout.item_confirm_orders_group, parent);
            viewHolderGroup = new ViewHolderGroup();
            viewHolderGroup.sdvStoreLogo = (SimpleDraweeView) convertView.findViewById(R.id.item_confirm_orders_group_iv_store_logo);
            viewHolderGroup.tvStoreName = (TextView) convertView.findViewById(R.id.item_confirm_orders_group_tv_store_name);
            convertView.setTag(viewHolderGroup);
        } else
            viewHolderGroup = (ViewHolderGroup) convertView.getTag();

        // 商店LOGO
        FrescoUtil.showImageSmall(groupList.get(groupPosition).getSupplierLogo(), viewHolderGroup.sdvStoreLogo);
        // 商店名称
        viewHolderGroup.tvStoreName.setText(groupList.get(groupPosition).getSupplierName());

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ViewHolderChild viewHolderChild;
        if (convertView == null) {
            convertView = inflate(R.layout.item_confirm_orders_child, parent);
            viewHolderChild = new ViewHolderChild();
            viewHolderChild.sdvImage = (SimpleDraweeView) convertView.findViewById(R.id.item_confirm_orders_child_iv_images);
            viewHolderChild.tvTitle = (TextView) convertView.findViewById(R.id.item_confirm_orders_child_tv_title);
            viewHolderChild.tvIntroduce = (TextView) convertView.findViewById(R.id.item_confirm_orders_child_tv_introduce);
            viewHolderChild.tvPrice = (TextView) convertView.findViewById(R.id.item_confirm_orders_child_tv_price);
            viewHolderChild.tvType = (TextView) convertView.findViewById(R.id.item_confirm_orders_child_tv_type);
            viewHolderChild.llparent = (RelativeLayout) convertView.findViewById(R.id.item_confirm_orders_child_parent_layout);
            convertView.setTag(viewHolderChild);
        } else
            viewHolderChild = (ViewHolderChild) convertView.getTag();

        // 商品图片
        FrescoUtil.showImageSmall(childList.get(groupPosition).get(childPosition).getItemLogo(), viewHolderChild.sdvImage);
        // 商品名称
        viewHolderChild.tvTitle.setText(childList.get(groupPosition).get(childPosition).getItemName());
        // 商品简介
        viewHolderChild.tvIntroduce.setText(childList.get(groupPosition).get(childPosition).getItemSummary());
        // 商品价格
        viewHolderChild.tvPrice.setText(String.format("￥ %s", childList.get(groupPosition).get(childPosition).getItemPrice()));
        // 商品类型
        viewHolderChild.tvType.setText(childList.get(groupPosition).get(childPosition).getItemUnit());

        /**
         * 根布局点击事件
         */
        viewHolderChild.llparent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                GotoUtil.gotoActivity((Activity) mContext, ProductDetailActivity.class, "productId", childList.get(groupPosition).get(childPosition).getItemId());
                ToastUtil.showToast(mContext, "根布局点击事件");
            }
        });


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public View inflate(int layoutId, ViewGroup parent) {
        return layoutInflater.inflate(layoutId, parent, false);
    }

    public class ViewHolderGroup {
        SimpleDraweeView sdvStoreLogo;
        TextView tvStoreName;
    }

    public class ViewHolderChild {
        SimpleDraweeView sdvImage; // 商品图片
        TextView tvTitle; // 商品名称
        TextView tvIntroduce; // 商品简介
        TextView tvPrice; // 商品价格
        TextView tvType; // 商品类型
        RelativeLayout llparent; // 根布局
    }
}
